package com.aviatorscript.avitorscriptmust.controller;

import com.aviatorscript.avitorscriptmust.annotation.AviatorValidation;
import com.aviatorscript.avitorscriptmust.common.Result;
import com.aviatorscript.avitorscriptmust.dto.ValidationResult;
import com.aviatorscript.avitorscriptmust.pojo.CreateOrderDTO;
import com.aviatorscript.avitorscriptmust.validation.AviatorValidationExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 测试Controller
 * 用于测试AviatorScript校验功能
 *
 * @author system
 */
@RestController
@RequestMapping("/api/test")
@Slf4j
public class TestController {
    
    @Autowired
    private AviatorValidationExecutor validationExecutor;
    
    /**
     * 手动校验测试接口
     */
    @PostMapping("/validate")
    public Result<ValidationResult> testValidation(@RequestBody CreateOrderDTO orderDTO) {
        log.info("手动校验测试: orderId={}", orderDTO.getOrderId());
        
        try {
            // 执行手动校验
            ValidationResult result = validationExecutor.validate(orderDTO);
            
            if (result.isValid()) {
                return Result.success(result, "校验通过");
            } else {
                return Result.validateError(result.getErrorMessage());
            }
            
        } catch (Exception e) {
            log.error("校验测试失败", e);
            return Result.error("校验测试失败: " + e.getMessage());
        }
    }
    
    /**
     * 注解校验测试接口（自动校验）
     */
    @PostMapping("/validate-auto")
    @AviatorValidation(message = "订单数据校验失败")
    public Result<String> testAutoValidation(@AviatorValidation @RequestBody CreateOrderDTO orderDTO) {
        log.info("注解校验测试通过: orderId={}", orderDTO.getOrderId());
        
        // 如果到达这里，说明校验已经通过
        return Result.success("订单处理成功，orderId=" + orderDTO.getOrderId());
    }
    
    /**
     * 禁用校验的测试接口
     */
    @PostMapping("/no-validate")
    @AviatorValidation(enabled = false)
    public Result<String> testNoValidation(@AviatorValidation(enabled = false) @RequestBody CreateOrderDTO orderDTO) {
        log.info("禁用校验测试: orderId={}", orderDTO.getOrderId());
        
        return Result.success("无校验处理成功，orderId=" + orderDTO.getOrderId());
    }
    
    /**
     * 模拟业务处理接口（不抛出异常，只记录错误）
     */
    @PostMapping("/validate-no-exception")
    public Result<String> testValidationNoException(@AviatorValidation(throwException = false) @RequestBody CreateOrderDTO orderDTO) {
        log.info("不抛出异常的校验测试: orderId={}", orderDTO.getOrderId());
        
        // 即使校验失败也不会抛出异常，会继续执行到这里
        return Result.success("处理完成（校验可能失败但未抛出异常）");
    }
    
    /**
     * 创建测试数据接口
     */
    @GetMapping("/create-test-data")
    public Result<CreateOrderDTO> createTestData() {
        CreateOrderDTO orderDTO = new CreateOrderDTO();
        orderDTO.setOrderId("TEST_ORDER_123");
        orderDTO.setCustomerId("12345");
        orderDTO.setTmsClient("TEST_CLIENT");
        
        // 创建购物车数据
        CreateOrderDTO.Box box = new CreateOrderDTO.Box();
        box.setBoxNo("BOX_001");
        
        CreateOrderDTO.Item item1 = new CreateOrderDTO.Item();
        item1.setItemId("ITEM_001");
        item1.setItemName("测试商品1");
        item1.setItemPrice(java.math.BigDecimal.valueOf(99.99));
        
        CreateOrderDTO.Item item2 = new CreateOrderDTO.Item();
        item2.setItemId("ITEM_002");
        item2.setItemName("测试商品2");
        item2.setItemPrice(java.math.BigDecimal.valueOf(199.99));
        
        box.setItemList(java.util.Arrays.asList(item1, item2));
        orderDTO.setCartList(java.util.Arrays.asList(box));
        
        return Result.success(orderDTO, "测试数据创建成功");
    }
    
    /**
     * 创建错误测试数据接口（用于测试校验失败）
     */
    @GetMapping("/create-error-data")
    public Result<CreateOrderDTO> createErrorData() {
        CreateOrderDTO orderDTO = new CreateOrderDTO();
        // 故意设置错误数据
        orderDTO.setOrderId(""); // 空字符串，应该校验失败
        orderDTO.setCustomerId("abc"); // 非数字，应该校验失败
        orderDTO.setTmsClient("TEST_CLIENT");
        
        // 创建购物车数据
        CreateOrderDTO.Box box = new CreateOrderDTO.Box();
        box.setBoxNo(""); // 空字符串，应该校验失败
        
        CreateOrderDTO.Item item = new CreateOrderDTO.Item();
        item.setItemId("ITEM_001");
        item.setItemName("测试商品");
        item.setItemPrice(java.math.BigDecimal.valueOf(-10)); // 负数，应该校验失败
        
        box.setItemList(java.util.Arrays.asList(item));
        orderDTO.setCartList(java.util.Arrays.asList(box));
        
        return Result.success(orderDTO, "错误测试数据创建成功");
    }
} 